package GUI;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import Run_System.*;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.util.ArrayList;
import java.util.regex.Pattern;

public class ContinueExercisesPanel extends JPanel {
    private  Exercises exercises;
    private ArrayList<Integer> answers;
    public ContinueExercisesPanel(){
        setLayout(null);

        JLabel label = new JLabel("还记得是哪天做的习题吗？"); //标题框，提醒小明根据日期选择习题
        Font font = new Font("微软雅黑",Font.BOLD,16);
        label.setFont(font);
        label.setHorizontalAlignment(SwingConstants.CENTER);
        label.setBounds(250,50,300,50);
        add(label);

        JList<String> exerciseFileList = new JList<String>(FileOperator.readExercisesFile().toArray(new String[0])); //读取所有存储的习题文件，以列表形式供小明选择
        exerciseFileList.setBounds(200,150,400,300);
        exerciseFileList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if(e.getClickCount() == 2){
                    JList<String> mylist = (JList<String>) e.getSource();
                    int index = mylist.getSelectedIndex();
                    String filePath = "src\\main\\resources\\"+mylist.getModel().getElementAt(index);
                    try {
                        exercises = FileOperator.readExercises(filePath);
                        String answerPath = filePath.replaceAll("exercises","answers");
                        answers = FileOperator.readAnswers(answerPath);
                        DisplayExercisesPanel panel = new DisplayExercisesPanel(exercises,answers);
                        removeAll();
                        add(panel);
                        validate();
                    }catch (Exception ex){
                        ex.printStackTrace();
                    }
                }
            }
        });
        add(exerciseFileList,BorderLayout.CENTER);

        JLabel label1 = new JLabel("双击下试试吧");
        label1.setFont(new Font("微软雅黑",Font.BOLD,16));
        label1.setHorizontalAlignment(SwingConstants.CENTER);
        label1.setBounds(250,500,300,50);
        add(label1);

    }

    public static void main(String[] args) {
    }

}
